package tripx.siteget.crawler;

import java.io.IOException;
import java.util.List;

import tripx.siteget.start.Client;
import tripx.update.Site;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlMeta;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlParagraph;

public class SiteCrawler {

	private String siteSpell;

	private String cityName;

	private Site site;

	private WebClient client;

	public SiteCrawler(String siteSpell, String cityName) {
		this.siteSpell = siteSpell;
		this.cityName = cityName;
		client = new WebClient(BrowserVersion.FIREFOX_17);
	}

	public void crawl() {
		HtmlPage page;
		try {
			page = client.getPage("http://lvyou.baidu.com/" + siteSpell);
			List<?> siteNameNodes = page
					.getByXPath("//*[@id=\"body\"]/div/article/header/section[1]/header/p");
			String siteName = "";
			if (siteNameNodes.size() > 0) {
				siteName = ((HtmlParagraph) (siteNameNodes.get(0)))
						.getTextContent();
			}

			List<?> introNodes = page
					.getByXPath("//*[@id=\"body\"]/div/section/article/div/div/section[1]/article/div");
			String intro = "";
			if (introNodes.size() > 0) {
				HtmlDivision introParagraph = (HtmlDivision) introNodes.get(0);
				Iterable<DomNode> paraNodes = introParagraph.getChildren();
				for (DomNode node : paraNodes) {
					intro += (node.getTextContent().trim());
				}
			}

			List<?> addressNodes = page
					.getByXPath("//*[@id=\"J-aside-info-address\"]/span[@class=\"val address-value\"]");
			String address = "";
			if (addressNodes.size() > 0) {
				address = ((HtmlElement) addressNodes.get(0)).getTextContent()
						.trim();
			}

			List<?> phoneNodes = page
					.getByXPath("//*[@id=\"J-aside-info-phone\"]/span[@class=\"val phone-value\"]");
			String phone = "";
			if (phoneNodes.size() > 0) {
				phone = ((HtmlElement) phoneNodes.get(0)).getTextContent()
						.trim();
			}

			List<?> urlImageNodes = page
					.getByXPath("//*[@id=\"J_slide-holder\"]/meta");
			String urlImage = "";
			if (urlImageNodes.size() > 0) {
				urlImage = ((HtmlMeta) urlImageNodes.get(0))
						.getContentAttribute();
			}
			Client.getInstance().uploadSiteImage(urlImage, cityName, siteName);
			site = new Site(cityName, siteName, address, intro, phone);
		} catch (FailingHttpStatusCodeException | IOException e) {

		}

	}

	public Site getSite() {
		return site;
	}
}
